package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.validation.TaskValidator;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.Logger;
import ge.telasi.tasks.ui.log.LoggerUtils;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 * @author dimitri
 */
public class PriorityPanel extends JPanel implements Loggable {

    public PriorityPanel() {
        initComponents();
        UIUtils.correctTitleBorderFont(jPanel1);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        rdCommon = new javax.swing.JRadioButton();
        rdLow = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        rdHigh = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        txtDueDate = new ge.telasi.tasks.ui.comp.CalendarField();
        jLabel4 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("შესრულების პრიორიტეტი"));

        buttonGroup1.add(rdCommon);
        rdCommon.setSelected(true);
        rdCommon.setText("ჩვეულებრივი");

        buttonGroup1.add(rdLow);
        rdLow.setText("დაბალი");

        jLabel1.setFont(jLabel1.getFont().deriveFont((jLabel1.getFont().getStyle() | java.awt.Font.ITALIC)));
        jLabel1.setForeground(java.awt.Color.gray);
        jLabel1.setText("დავალება რომლის შესრულება არაა სასწრაფო");

        jLabel2.setFont(jLabel2.getFont().deriveFont((jLabel2.getFont().getStyle() | java.awt.Font.ITALIC)));
        jLabel2.setForeground(java.awt.Color.gray);
        jLabel2.setText("საწყისი პრიორიტეტი");

        buttonGroup1.add(rdHigh);
        rdHigh.setText("მაღალი");

        jLabel3.setFont(jLabel3.getFont().deriveFont((jLabel3.getFont().getStyle() | java.awt.Font.ITALIC)));
        jLabel3.setForeground(java.awt.Color.gray);
        jLabel3.setText("სასწრაფო და გადაუდებელი დავალებები");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rdLow)
                            .addComponent(rdCommon)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE))
                            .addComponent(rdHigh))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rdLow)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdCommon)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdHigh)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        txtDueDate.setName("Task.dueDate"); // NOI18N

        jLabel4.setText("შესრულების ბოლო თარიღი:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDueDate, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4)
                    .addComponent(txtDueDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton rdCommon;
    private javax.swing.JRadioButton rdHigh;
    private javax.swing.JRadioButton rdLow;
    private ge.telasi.tasks.ui.comp.CalendarField txtDueDate;
    // End of variables declaration//GEN-END:variables

    private Logger logger;
    private Task task;

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
        reset();
    }
    
    public Logger getLogger() {
        return logger;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public JComponent[] allFields() {
        return new JComponent[] {rdLow, rdHigh, rdCommon, txtDueDate};
    }

    protected void clearLogger() {
        LoggerUtils.clearLogger(logger, allFields());
    }

    protected boolean validateForm() {
        clearLogger();
        try {
            Task formTask = new Task();
            fillTask(formTask);
            new TaskValidator().validateDueDate(formTask.getDueDate());
            return true;
        } catch (Exception ex) {
            LoggerUtils.manageException(this, logger, ex, allFields());
            return false;
        }
    }
    
    public void fillTask(Task task) {
        if (rdLow.isSelected()) {
            task.setPriority(Task.PRIORITY_LOW);
        } else if (rdHigh.isSelected()) {
            task.setPriority(Task.PRIORITY_HIGH);
        } else {
            task.setPriority(Task.PRIORITY_NORMAL);
        }
        task.setDueDate(txtDueDate.getDate());
    }

    private void reset() {
        switch (task.getPriority()) {
            case Task.PRIORITY_HIGH:
                rdHigh.setSelected(true);
                break;
            case Task.PRIORITY_LOW:
                rdLow.setSelected(true);
                break;
            default:
                rdCommon.setSelected(true);
                break;
        }
        txtDueDate.setDate(task.getDueDate());
    }

}
